<?php

namespace app\api\controller;

use think\Response;
use think\facade\Db;
use think\Request;

class Coupon extends Wap
{
    /**
     * 优惠券列表
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index(Request $request)
    {
        if (request()->isPost()) {
            $user = $this->checkToken();
            $post = $request->param();
            if (isset($post['store_id'])) {
                if ($post['store_id'] > 0) {
                    $data = array();
                    $list = Db::name('coupon_area')->where('store_id', $post['store_id'])->select();

                    if (count($list) > 0) {
                        foreach ($list as $i => $v) {
                            $coupon_id[] = $v['coupon_id'];
                        }
                        $list = array();
                        if (count($coupon_id) > 0) {
                            $list = Db::name('coupon')->where('mark', 1)->where('store_id', '')->where('end_time', '>', time())->where('id', 'in', $coupon_id)->where('status', 1)->select();

                        }

                        foreach ($list as $i => $v) {
                            $store_name = $this->getUserRoleList($v['id']);
                            $count = Db::name('mini_user_coupon')->where('mark', 1)->where('user_id', $user['id'])->where('coupon_id', $v['id'])->count();
                            if ($count >= $v['user_num']) {
                                $status = 1;
                            } else {
                                $status = 0;
                            }

                            $data[] = array(
                                'id' => $v['id'],
                                'title' => $v['title'],
                                'price' => $v['price'],
                                'add_time' => date('Y-m-d', $v['add_time']),
                                'end_time' => date('Y-m-d', $v['end_time']),
                                'store' => $store_name,
                                'image_url' => ($v['image_url']),
                                'status' => $status,


                            );
                        }
                        $arr['code'] = 200;
                        $arr['data'] = $data;
                        $arr['message'] = '获取成功';
                        Response::create($arr, 'json')->send();
                        exit();
                    } else {
                        $arr['code'] = 200;
                        $arr['data'] = $data;
                        $arr['message'] = '获取成功';
                        Response::create($arr, 'json')->send();
                        exit();
                    }
                }
            }
            $list = Db::name('coupon')->where('mark', 1)->where('end_time', '>', time())->where('store_id', '')->where('status', 1)->select();
            $data = array();
            foreach ($list as $i => $v) {
                $store_name = $this->getUserRoleList($v['id']);
                $count = Db::name('mini_user_coupon')->where('mark', 1)->where('user_id', $user['id'])->where('coupon_id', $v['id'])->count();
                if ($count >= $v['user_num']) {
                    $status = 1;
                } else {
                    $status = 0;
                }

                $data[] = array(
                    'id' => $v['id'],
                    'title' => $v['title'],
                    'price' => $v['price'],
                    'add_time' => date('Y-m-d', $v['add_time']),
                    'end_time' => date('Y-m-d', $v['end_time']),
                    'store' => $store_name,
                    'image_url' => ($v['image_url']),
                    'status' => $status,


                );
            }
            $arr['code'] = 200;
            $arr['data'] = $data;
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();
        }

    }


    /**
     * 优惠券列表
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function coupontc(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            if (isset($post['store_id'])) {
                if ($post['store_id'] > 0) {


                    $list = Db::name('coupon_area')->alias('a')
                        ->join('coupon c', 'c.id=a.coupon_id')
                        ->where('a.store_id', $post['store_id'])
                        ->where('c.mark', 1)
                        ->where('c.status', 1)
                        ->where('c.store_id', '')
                        ->where('c.end_time', '>', time())
                        ->order('c.price asc')->sum('c.price');
                    $user = $this->checkToken();
                    $recharge_order = Db::name('customer_recharge_order')->where('store_id', $post['store_id'])->where('user_id', $user['id'])->where('status', 1)->find();

                    $arr['code'] = 200;
                    $arr['data'] = $list;
                    $arr['room'] = Db::name('mini_room_list')->where('id', $post['store_id'])->find();
                    $arr['vip'] = empty($recharge_order) ? 0 : 1;
                    $arr['message'] = '获取成功';
                    Response::create($arr, 'json')->send();
                    exit();
                }
            } else {
                $arr['code'] = 200;
                $arr['data'] = '';
                $arr['message'] = '获取成功';
                Response::create($arr, 'json')->send();
                exit();
            }

        }

    }

    /**
     * 查询适用门店
     * @param $userId
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getUserRoleList($userId)
    {
        $roleList = Db::name('coupon_area')->alias("ur")
            ->field('r.room_name')
            ->join('mini_room_list r', 'ur.store_id=r.id')
            ->distinct(true)
            ->where('ur.coupon_id', '=', $userId)
            ->select()->toArray();
        return $roleList;
    }


    /**
     * 领取优惠卷
     * @param Request $request
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public
    function receive(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            $user = $this->checkToken();
            if (!isset($post['coupon_id'])) {
                $arr['code'] = 1001;
                $arr['message'] = '参数错误';
                Response::create($arr, 'json')->send();
                exit();
            }
            $user = Db::name('member')->where('id', $user['id'])->find();
            // 查询当前用书是否已利股权
            $coupon = Db::name('coupon')->where('id', $post['coupon_id'])->find();
            $count = Db::name('mini_user_coupon')->where('user_id', $user['id'])->where('mark', 1)->where('coupon_id', $post['coupon_id'])->count();


            if ($count < intval($coupon['user_num'])) {
                if ($coupon['receive_num'] < $coupon['num']) {

                    $dataCoupon = array(
                        'coupon_id' => $coupon['id'],
                        'user_id' => $user['id'],
                        'type' => 1,
                        'end_time' => $coupon['end_time'],
                        'add_time' => $coupon['add_time'],
                        'hours_add_time' => $coupon['hours_add_time'],
                        'hours_end_time' => $coupon['hours_end_time'],
                        'price' => $coupon['price'],
                    );
                    if (Db::name('mini_user_coupon')->insert($dataCoupon)) {
                        // 更新领取数量
                        $coi = array(
                            'surplus_num' => $coupon['num'] - $coupon['receive_num'] - 1,
                            'receive_num' => $coupon['receive_num'] + 1,
                        );
                        Db::name('coupon')->where('id', $coupon['id'])->update($coi);
                        $arr['code'] = 200;
                        $arr['message'] = '领取成功';
                        Response::create($arr, 'json')->send();
                        exit();
                    }
                }
            } else {
                $arr['code'] = 1001;
                $arr['message'] = '不可重复领取';
                Response::create($arr, 'json')->send();
                exit();
            }
        }
    }


    /**
     * 用户优惠券
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function userCoupon(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            $user = $this->checkToken();
            if ($post['status'] == 0) {
                $list = Db::name('mini_user_coupon')->where('user_id', $user['id'])->where('mark', 1)->where('end_time', '>', time())->where('status', $post['status'])->where('mark', 1)->select();

            } elseif ($post['status'] == 2) {
                $list = Db::name('mini_user_coupon')->where('user_id', $user['id'])->where('mark', 1)->where('end_time', '<', time())->where('status', 0)->where('mark', 1)->select();

            } else {
                $list = Db::name('mini_user_coupon')->where('user_id', $user['id'])->where('mark', 1)->where('status', $post['status'])->where('mark', 1)->select();

            }
            $data = array();
            if (count($list) > 0) {
                foreach ($list as $i => $v) {

                    $coupon = Db::name('coupon')->where('id', $v['coupon_id'])->find();
                    if ($coupon) {
                        $data[] = array(
                            'id' => $v['id'],
                            'price' => $v['price'],
                            'title' => $coupon['title'],
                            'add_time' => date('Y.m.d', $v['add_time']) . '-' . date('Y.m.d', $v['end_time']),
                            'store_name' => $this->getUserRoleList($v['coupon_id']),
                            'hours_add_time' => $v['hours_add_time'],
                            'hours_end_time' => $v['hours_end_time'],
                            'status' => $post['status']
                        );
                    }

                }
            }

            $arr['code'] = 200;
            $arr['data'] = $data;
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();

        }
    }

    /**
     * 优惠券列表
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function pricecoupon(Request $request)
    {
        if (request()->isPost()) {
            $user = $this->checkToken();
            $post = $request->param();

            $list = Db::name('pricecoupon')->where('mark', 1)->where('store_id', $post['store_id'])->where('status', 1)->select();
            $data = [];
            foreach ($list as $i => $v) {
                $store_name = $this->getUserRoleList($v['id']);
                $data[] = array(
                    'id' => $v['id'],
                    'title' => $v['title'],
                    'price' => $v['price'],
                    'originalprice' => $v['originalprice'],
                    'shichang' => $v['shichang'],
                    'add_time' => date('Y-m-d', $v['add_time']),
                    'end_time' => date('Y-m-d', $v['end_time']),
                    'hours_add_time' => $v['hours_add_time'],
                    'hours_end_time' => $v['hours_end_time'],
                    'store' => $store_name,
                    'image_url' => ($v['image_url']),
                );
            }
            $arr['code'] = 200;
            $arr['data'] = $data;
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();

        }
    }


    /**
     * 优惠券列表
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function pricecouponinfo(Request $request)
    {
        if (request()->isPost()) {
            $user = $this->checkToken();
            $post = $request->param();

            $list = Db::name('pricecoupon')->where('mark', 1)->where('id', $post['id'])
                ->where('status', 1)->find();
            $list['add_time'] = date('Y-m-d', $list['add_time']);
            $list['end_time'] = date('Y-m-d', $list['end_time']);
            $list['store'] = Db::name('mini_room_list')->where('id', $list['store_id'])->select();

            $list['yhj'] = round($list['originalprice'] - $list['price'], 2);
            $list['crad'] = Db::name('customer_course')
                ->whereIn('id', $list['course_id'])->column('name', 'id');
            $list['crad'] = implode(',', $list['crad']);
            $arr['code'] = 200;
            $arr['data'] = $list;
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();

        }
    }

    /**
     * 充值
     * @param Request $request
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function couponOrder(Request $request)
    {
        if (request()->isPost()) {
            $user = $this->checkToken();
            $post = $request->param();

            if (!isset($post['id'])) {
                $arr['code'] = 1001;
                $arr['message'] = '参数错误';
                Response::create($arr, 'json')->send();
                exit();
            }
            Db::name('customer_coupon_order')->where('status', 0)->where('user_id', $user['id'])->delete();
            $recharge = Db::name('pricecoupon')->where('status', 1)->where('id', $post['id'])->find();
            $data['order_on'] = $this->orderOn();
            $data['create_time'] = time();
            $data['price'] = $recharge['price'];
            $data['level_id'] = $post['id'];

            $data['user_id'] = $user['id'];
            $data['store_id'] = $recharge['store_id'];

            if (isset($recharge['store_id'])) {
                $room = Db::name('mini_room_list')->where('id', $recharge['store_id'])->find();

                $data['store_id'] = $recharge['store_id'];
                $da['if_fz'] = 0;
                if ($room['sub_mch_id'] >0) {
                    $da['if_fz'] = 1;
                    $data['if_fz'] = 1;
                }
            }

            if (Db::name('customer_coupon_order')->insert($data)) {

                $arr['code'] = 200;
                $da['order_on'] = $data['order_on'];
                $arr['data'] = $da;
                $arr['message'] = '获取成功';
                Response::create($arr, 'json')->send();
                exit();
            }
        }
    }


    /**
     * 用户优惠券
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function userpriceCoupon(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            $user = $this->checkToken();
            $list = Db::name('mini_user_pricecoupon')->where('order_on', '<>', null)->where('user_id', $user['id'])->where('mark', 1)->where('status', $post['status'])->where('mark', 1)->select();
            $data = array();
            if (count($list) > 0) {
                foreach ($list as $i => $v) {

                    $coupon = Db::name('pricecoupon')->where('id', $v['coupon_id'])->find();
                    $room_name = Db::name('mini_room_list')->where('id', $v['store_id'])->column('room_name');
                    $crad = Db::name('customer_course')
                        ->whereIn('id', $v['course_id'])->column('name', 'id');
                    if ($coupon) {
                        $data[] = array(
                            'id' => $v['id'],
                            'price' => $v['price'],
                            'title' => $coupon['title'],
                            'add_time' => date('Y.m.d', $v['add_time']) . '-' . date('Y.m.d', $v['end_time']),
                            'store_name' => $room_name[0],
                            'str_time' => $v['hours_add_time'] . '点-' . $v['hours_end_time'] . '点',
                            'shichang' => $v['shichang'],
                            'crad' => implode(',', $crad),
                            'status' => $v['status']
                        );
                    }

                }
            }

            $arr['code'] = 200;
            $arr['data'] = $data;
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();

        }
    }

    /**
     * 使用优惠券
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function priceCouponOrder(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            $user = $this->checkToken();
            if ($post['xd'] == 1) {
                $orderInfo = Db::name('customer_renewal_order')->where('order_on', $post['order_on'])->find();
            } else {
                $orderInfo = Db::name('customer_order')->where('order_on', $post['order_on'])->find();
            }

            if ($orderInfo) {
                $shichang = $orderInfo['shi'];

                $add_time = date('H', $orderInfo['add_time']);
                $end_time = date('H', $orderInfo['end_time']);
                if ($shichang >= 8) {

                    if ($add_time == '00') {
                        $add_time = 24;
                    } elseif ($add_time == '01') {
                        $add_time = 25;
                    }

                }
                $seat_id = $orderInfo['seat_id'];
                $userCoupon = Db::name('mini_user_pricecoupon')->where(function ($query) use ($seat_id) {
                    $query->whereFindInSet('course_id', $seat_id)
                        ->whereOr('course_id', '=', '')
                        ->whereOr('course_id', Null);
                })->where('order_on', '<>', null)->where('shichang', $shichang)
                    ->where('hours_add_time', '<=', $add_time)
                    ->where('hours_end_time', '>=', $end_time)
                    ->where('store_id', $post['store_id'])->where('status', 0)->where('user_id', $user['id'])->select();

                $data = array();
                foreach ($userCoupon as $i => $v) {
                    $room_name = Db::name('mini_room_list')->where('id', $v['store_id'])->column('room_name');

                    $coupon = Db::name('pricecoupon')->where('id', $v['coupon_id'])->find();
                    $crad = Db::name('customer_course')
                        ->whereIn('id', $v['course_id'])->column('name', 'id');
                    $data[] = array(
                        'id' => $v['id'],
                        'price' => $v['price'],
                        'title' => $coupon['title'],
                        //   'add_time' => date('Y.m.d', $v['add_time']) . '-' . date('Y.m.d', $v['end_time']),
                        'store_name' => $room_name[0],
                        'str_time' => $v['hours_add_time'] . '点-' . $v['hours_end_time'] . '点',
                        'shichang' => $v['shichang'],
                        'crad' => implode(',', $crad),
                        'status' => $v['status']
                    );

                }

                $arr['code'] = 200;
                $arr['data'] = $data;
                $arr['message'] = '获取成功';
                Response::create($arr, 'json')->send();
                exit();
            } else {
                $arr['code'] = 1001;

                $arr['message'] = '请重新下单';
                Response::create($arr, 'json')->send();
                exit();
            }

        }
    }


    /**
     * 周卡列表
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function cardcoupon(Request $request)
    {
        if (request()->isPost()) {
            $user = $this->checkToken();
            $post = $request->param();

            $list = Db::name('card')->where('mark', 1)->where('store_id', $post['store_id'])->where('status', 1)->select();
            $data = [];
            foreach ($list as $i => $v) {
                $store_name = $this->getUserRoleList($v['id']);
                $data[] = array(
                    'id' => $v['id'],
                    'title' => $v['title'],
                    'price' => $v['price'],
                    'originalprice' => $v['originalprice'],
                    'shichang' => $v['shichang'],
                    'add_time' => date('Y-m-d', $v['add_time']),
                    'end_time' => date('Y-m-d', $v['end_time']),
                    'hours_add_time' => $v['hours_add_time'],
                    'hours_end_time' => $v['hours_end_time'],
                    'store' => $store_name,
                    'image_url' => ($v['image_url']),
                );
            }
            $arr['code'] = 200;
            $arr['data'] = $data;
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();

        }
    }


    /**
     * 卡卷详情
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function cardcouponinfo(Request $request)
    {
        if (request()->isPost()) {
            $user = $this->checkToken();
            $post = $request->param();

            $list = Db::name('card')->where('mark', 1)->where('id', $post['id'])
                ->where('status', 1)->find();
            $list['add_time'] = date('Y-m-d', $list['add_time']);
            $list['end_time'] = date('Y-m-d', $list['end_time']);
            $list['store'] = Db::name('mini_room_list')->where('id', $list['store_id'])->select();

            $list['yhj'] = round($list['originalprice'] - $list['price'], 2);
            $list['crad'] = Db::name('customer_course')
                ->whereIn('id', [$list['course_id']])->column('name', 'id');

            $list['crad'] = implode(',', $list['crad']);
            $arr['code'] = 200;
            $arr['data'] = $list;
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();

        }
    }


    /**
     * 创建卡卷
     * @param Request $request
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function cardcouponOrder(Request $request)
    {
        if (request()->isPost()) {
            $user = $this->checkToken();
            $post = $request->param();

            if (!isset($post['id'])) {
                $arr['code'] = 1001;
                $arr['message'] = '参数错误';
                Response::create($arr, 'json')->send();
                exit();
            }
            Db::name('customer_cardcoupon_order')->where('status', 0)->where('user_id', $user['id'])->delete();
            $recharge = Db::name('card')->where('status', 1)->where('id', $post['id'])->find();
            $data['order_on'] = $this->orderOn();
            $data['create_time'] = time();
            $data['price'] = $recharge['price'];
            $data['level_id'] = $post['id'];
            $data['user_id'] = $user['id'];
            $data['store_id'] = $recharge['store_id'];
            $data['num'] = $recharge['number'];
            if (isset($recharge['store_id'])) {
                $room = Db::name('mini_room_list')->where('id', $recharge['store_id'])->find();
                $data['store_id'] = $recharge['store_id'];
                $da['if_fz'] = 0;
                if ($room['sub_mch_id'] >0) {
                    $da['if_fz'] = 1;
                    $data['if_fz'] = 1;
                }
            }
            if (Db::name('customer_cardcoupon_order')->insert($data)) {

                $arr['code'] = 200;
                $da['order_on'] = $data['order_on'];
                $arr['data'] = $da;
                $arr['message'] = '获取成功';
                Response::create($arr, 'json')->send();
                exit();
            }
        }
    }


    /**
     * 使用优惠券
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function cardCouponOrderlist(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            $user = $this->checkToken();
            if ($post['xd'] == 1) {
                $orderInfo = Db::name('customer_renewal_order')->where('order_on', $post['order_on'])->find();
            } else {
                $orderInfo = Db::name('customer_order')->where('order_on', $post['order_on'])->find();
            }

            if ($orderInfo) {
                $shichang = $orderInfo['shi'];

                $add_time = date('H', $orderInfo['add_time']);
                $end_time = date('H', $orderInfo['end_time']);
                if ($shichang >= 8) {

                    if ($add_time == '00') {
                        $add_time = 24;
                    } elseif ($add_time == '01') {
                        $add_time = 25;
                    }

                }
                $seat_id = $orderInfo['seat_id'];
                $userCoupon = Db::name('mini_user_cardcoupon')->where(function ($query) use ($seat_id) {
                    $query->whereFindInSet('course_id', $seat_id)
                        ->whereOr('course_id', '=', '')
                        ->whereOr('course_id', Null);
                })->where('order_on', '<>', null)->where('shichang', $shichang)
                    ->where('hours_add_time', '<=', $add_time)
                    ->where('hours_end_time', '>=', $end_time)
                    ->where('store_id', $post['store_id'])->where('status', 0)->where('add_time', '<', time())->where('end_time', '>', time())->where('user_id', $user['id'])->select();

                $data = array();
                foreach ($userCoupon as $i => $v) {
                    $room_name = Db::name('mini_room_list')->where('id', $v['store_id'])->column('room_name');

                    $coupon = Db::name('card')->where('id', $v['coupon_id'])->find();
                    $crad = Db::name('customer_course')
                        ->whereIn('id', $v['course_id'])->column('name', 'id');
                    $data[] = array(
                        'id' => $v['id'],
                        'price' => $v['price'],
                        'title' => $coupon['title'],
                        'add_time' => date('Y.m.d', $v['end_time']),
                        'store_name' => $room_name[0],
                        'str_time' => $v['hours_add_time'] . '点-' . $v['hours_end_time'] . '点',
                        'shichang' => $v['shichang'],
                        'num' => $v['num'],
                        'crad' => implode(',', $crad),
                        'status' => $v['status']
                    );

                }

                $arr['code'] = 200;
                $arr['data'] = $data;
                $arr['message'] = '获取成功';
                Response::create($arr, 'json')->send();
                exit();
            } else {
                $arr['code'] = 1001;

                $arr['message'] = '请重新下单';
                Response::create($arr, 'json')->send();
                exit();
            }

        }
    }


    /**
     * 用户卡券
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function usercardCoupon(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            $user = $this->checkToken();

            if ($post['status'] == 0) {
                $list = Db::name('mini_user_cardcoupon')->where('order_on', '<>', null)->where('user_id', $user['id'])->where('mark', 1)->where('end_time', '>', time())->where('status', $post['status'])->where('mark', 1)->select();

            } elseif ($post['status'] == 2) {
                $list = Db::name('mini_user_cardcoupon')->where('order_on', '<>', null)->where('user_id', $user['id'])->where('mark', 1)->where('end_time', '<', time())->where('status', 0)->where('mark', 1)->select();

            } else {
                $list = Db::name('mini_user_cardcoupon')->where('order_on', '<>', null)->where('user_id', $user['id'])->where('mark', 1)->where('status', $post['status'])->where('mark', 1)->select();

            }

            $data = array();
            if (count($list) > 0) {
                foreach ($list as $i => $v) {

                    $coupon = Db::name('card')->where('id', $v['coupon_id'])->find();
                    $room_name = Db::name('mini_room_list')->where('id', $v['store_id'])->column('room_name');
                    $crad = Db::name('customer_course')
                        ->whereIn('id', $v['course_id'])->column('name', 'id');
                    if ($coupon) {
                        $data[] = array(
                            'id' => $v['id'],
                            'price' => $v['price'],
                            'title' => $coupon['title'],
                            'add_time' => $v['end_time'] == '' ? '' : date('Y.m.d', $v['end_time']),
                            'store_name' => $room_name[0],
                            'str_time' => $v['hours_add_time'] . '点-' . $v['hours_end_time'] . '点',
                            'shichang' => $v['shichang'],
                            'num' => $v['num'],
                            'crad' => implode(',', $crad),
                            'status' => $post['status']
                        );
                    }

                }
            }

            $arr['code'] = 200;
            $arr['data'] = $data;
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();

        }
    }

}